<template>
  <div class="register">
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
      <h3 class="title">晨禾陪诊会员注册</h3>
      <el-form-item prop="phone">
        <el-input v-model="registerForm.phone" type="text" auto-complete="off" placeholder="手机号">
          <svg-icon slot="prefix" icon-class="phone" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="nickName">
        <el-input v-model="registerForm.nickName" type="text" auto-complete="off" placeholder="姓名">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="invitationCode">
        <el-input
          v-model="registerForm.invitationCode" placeholder="请输入验证码" :disabled="invitationCodeFlag">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="registerForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="register-code">
          <img :src="codeUrl" @click="getCode" class="register-code-img"/>
        </div>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleRegister"
        >
          <span v-if="!loading">注 册</span>
          <span v-else>注 册 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="qrcode" id="qrcode-container">
      <!--<div class="qrcode-text">长按二维码跳转小程序</div>-->
      <img src="../assets/logo/qrcode.jpg">
    </div>
    <div class="el-register-footer">
      <span>Copyright © 2018-2024 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg ,appletQrcodeRegister, wechatConfig } from "@/api/login";
import QRCode from 'qrcodejs2';
export default {
  name: "Register",
  data() {
    const validPhone = (rule, value, callback) => {
      const phoneRegex = /^((\+?86)|(\(\+86\)))?1[3-9]\d{9}|(\d{3}-\d{8})|(\d{4}-\d{7})$/;
      if (phoneRegex.test(value)) {
        callback();
      } else {
        callback(new Error("手机号输入有误"));
      }
    };
    return {
      invitationCodeFlag: false,
      codeUrl: "",
      registerForm: {
        phone: "",
        nickName: "",
        invitationCode: "",
        code: "",
        uuid: ""
      },
      registerRules: {
        phone: [
          { required: true, trigger: "blur", message: "请输入您的手机号" },
          { required: true, validator: validPhone, trigger: "blur" }
        ],
        nickName: [
          { required: true, trigger: "blur", message: "请输入您的姓名" },
          { min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaEnabled: true
    };
  },
  created() {
    console.log("created............")
    this.getCode();
    // 获得路由上的值
    if(this.$route.query.invitationCode){
      this.registerForm.invitationCode = this.$route.query.invitationCode
      this.invitationCodeFlag = true;
    }

  },
  mounted() {
    console.log("mounted............")
    this.getWechatConfig();
  },
  methods: {
    // 获取微信配置信息的方法
    getWechatConfig() {
      wechatConfig('https://wx.chenhejt.cn/applet_qrcode_register').then(config=>{
        console.log("参数数据",config);
        wx.config({
          debug: false,
          appId: config.appId,
          timestamp: config.timestamp,
          nonceStr: config.nonceStr,
          signature: config.signature,
          jsApiList: ['miniProgram.navigateTo']
        });
      })

    },
    jumpToMiniProgram() {
      console.log("跳转")
      // 这里需要先配置微信开发接口的参数
      wx.miniProgram.navigateTo({
        appId: 'wxa34d3e392553c1ee',
        path: '/pages/index/index',
        success: res=> {
          console.log('跳转成功', res);
        },
        fail: err=>{
          console.error('跳转失败', err);
        }
      });
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.registerForm.uuid = res.uuid;
        }
      });
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          appletQrcodeRegister(this.registerForm).then(res => {
            console.log("返回结果",res);
            const phone = this.registerForm.phone;
            this.$modal.confirm('注册成功，是否打开晨禾陪诊小程序？').then(()=> {
              const qrcodeContainer = document.getElementById('qrcode-container');
              qrcodeContainer.style.display="block"
            }).then(() => {
              this.loading = false;
            }).catch(() => {});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          })
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.qrcode{
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 400px;
  height: 400px;
  display: none;
}
.qrcode img{
  width: 100%;
  height: 100%
}
.qrcode .qrcode-text{
  height: 50px;line-height: 50px;text-align: center;font-size: 18px;
}
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.register-code-img {
  height: 38px;
}
</style>
